package com.im.chat.service.impl;

import com.im.chat.base.BasePageServiceImpl;
import com.im.chat.dao.ChatRecordMapper;
import com.im.chat.dao.ChatSessionMapper;
import com.im.chat.model.ChatRecord;
import com.im.chat.model.ChatSession;
import com.im.chat.service.ChatRecordService;
import com.im.chat.util.ChatConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ChatRecordServiceImpl extends BasePageServiceImpl<ChatRecordMapper, ChatRecord> implements ChatRecordService {
    @Autowired
    private ChatRecordMapper chatRecordMapper;

    @Autowired
    private ChatSessionMapper chatSessionMapper;

    @Override
    protected void initMapper() {
        this.mapper = chatRecordMapper;
    }

    @Override
    public List<ChatRecord> selectByMember(long chatId, String requestTimePoint,int limit) {
        Map<String,Object> map = new HashMap<>();
        map.put("chatId",chatId);
        if(requestTimePoint.equals("n")){
            map.put("isFirstTime","");
        }else{
            map.put("requestTimePoint",requestTimePoint);
        }
        map.put("rollType","up");
        map.put("isDel", ChatConstant.IS_DEL_N);
        map.put("limit",limit);

        map.put("sortParamString", "create_time desc");

        return chatRecordMapper.selectByMap(map);
    }

    @Override
    public List<ChatRecord> selectByAdmin(long chatId, String requestTimePoint, String rollType,int limit) {
        Map<String,Object> map = new HashMap<>();
        map.put("chatId",chatId);
        map.put("rollType",rollType);
        if(requestTimePoint.equals("n")){
            map.put("isFirstTime","");
        }else{
            map.put("requestTimePoint",requestTimePoint);
        }

        map.put("isDel", ChatConstant.IS_DEL_N);
        map.put("limit",limit);
        if(rollType.equals("down")){
            map.put("sortParamString", "create_time asc");
        }
        if(rollType.equals("up")){
            map.put("sortParamString", "create_time desc");
        }

        return chatRecordMapper.selectByMap(map);
    }

    @Override
    public List<ChatRecord> searchByMember(long chatId,  String keyWord, Integer pageIndex, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("chatId",chatId);
        //map.put("memberId",memberId);
        //map.put("requestTimePoint",requestTimePoint);
        map.put("isDel", ChatConstant.IS_DEL_N);
        map.put("keyWord", keyWord);
        ///** 启始页-位置 */
        Integer start = (pageIndex - 1) * pageSize;
        /** 每页大小  */
        Integer limit = pageSize;
        map.put("start",start);
        map.put("limit",limit);
        map.put("sortParamString", "create_time desc");
        return chatRecordMapper.selectByMap(map);
    }

    @Override
    public List<ChatRecord> searchByAdmin(long chatId,  String keyWord, Integer pageIndex, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("chatId",chatId);
       // map.put("adminId",adminId);
        //map.put("requestTimePoint",requestTimePoint);
        map.put("isDel", ChatConstant.IS_DEL_N);
        map.put("keyWord", keyWord);
        ///** 启始页-位置 */
        Integer start = (pageIndex - 1) * pageSize;
        /** 每页大小  */
        Integer limit = pageSize;
        map.put("start",start);
        map.put("limit",limit);
        map.put("sortParamString", "create_time desc");
        return chatRecordMapper.selectByMap(map);
    }

    @Override
    public int delRecords(long chatId,String msgUniqueCodes) {
        List<String> list = Arrays.asList(msgUniqueCodes.split(","));
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("chatId", chatId);
        map.put("list", list);
        if(list.size()>0){
            return chatRecordMapper.updateDelStatusByMsgUniqueCode(map);
        }else{
            return 0;
        }

    }

    @Override
    public int updateMemberUnReadRecord(Long chatId, Long memberId) {
        ChatSession chatSession = chatSessionMapper.getChatSessionByChatId(chatId);
        if(chatSession!=null){
            Long adminId = chatSession.getAdminId();
            return chatRecordMapper.updateMemberUnReadRecordByAdminId(chatId,adminId);
        }else{
            return 0;
        }
    }

    @Override
    public int updateAdminUnReadRecord(Long chatId, Long adminId) {
        ChatSession chatSession = chatSessionMapper.getChatSessionByChatId(chatId);
        if(chatSession!=null){
            Long memberId = chatSession.getMemberId();
            return chatRecordMapper.updateAdminUnReadRecordByMemberId(chatId,memberId);
        }else{
            return 0;
        }
    }

    @Override
    public int getMemberUnReadRecord(Long chatId, Long memberId) {
        ChatSession chatSession = chatSessionMapper.getChatSessionByChatId(chatId);
        if(chatSession!=null){
            Long adminId = chatSession.getAdminId();
            return chatRecordMapper.getMemberUnReadRecordByAdminId(chatId,adminId);
        }else{
            return 0;
        }
    }

    @Override
    public int getAdminUnReadRecord(Long chatId, Long adminId) {
        ChatSession chatSession = chatSessionMapper.getChatSessionByChatId(chatId);
        if(chatSession!=null){
            Long memberId = chatSession.getMemberId();
            return chatRecordMapper.getAdminUnReadRecordByMemberId(chatId,memberId);
        }else{
            return 0;
        }
    }

    @Override
    public List<Long> getChatIdsBySearchNameAndAdminId(String searchName, Long adminId) {
        return chatRecordMapper.getChatIdsBySearchNameAndAdminId(searchName,adminId);
    }

    @Override
    public List<ChatRecord> getChatRecordBySearchName(String searchName, Long adminId, Long chatId) {
        return chatRecordMapper.getChatRecordBySearchName(searchName, adminId, chatId);
    }
}
